package com.uchengwang.ui.base_ui.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.uchengwang.R;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.listener.MyConnectionListener;
import com.uchengwang.ui.utils.AppStatusManager;
import com.uchengwang.ui.utils.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Created by U城网 on 2017/6/21.
 */

public class AllBaseActivity extends AutoLayoutActivity{

    private MyConnectionListener listener ;
    public boolean isInitMode = true;//是否初始化设置状态栏字体是黑色

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.d("jjf", "app强杀="+UChengApplication.nickName+"=id="+UChengApplication.uid);

        if (isInitMode){
            StatusBarUtil.getUtil().setStatusBarColor(this, R.color.white);
            StatusBarUtil.getUtil().StatusBarLightMode(this);
        }

        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏

        switch (AppStatusManager.getInstance().getAppStatus()) {
            case AppStatusManager.STATUS_FORCE_KILLED://应用被强杀 重启应用
                restartApp();
                break;
        }

        UChengApplication.instance.addActivity(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面
        MobclickAgent.onResume(this);

        listener = new MyConnectionListener(this);
        //融云注册一个监听连接状态的listener
        RongIM.setConnectionStatusListener(listener);
    }

    @Override
    protected void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd(getClass().getSimpleName()); //保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息。"SplashScreen"为页面名称，可自定义
        MobclickAgent.onPause(this);

        if (UChengApplication.getHttpClient() != null){
            //取消所有请求
            UChengApplication.getHttpClient().dispatcher().cancelAll();
            HttpManager.newInstance(this).setNull();
        }

        if (listener != null){
            RongIM.setConnectionStatusListener(null);
            listener = null;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        List<Activity> l = new ArrayList<>();
        List<Activity> list = UChengApplication.instance.getList();
        if (list != null && list.size() >0){
            for (Activity activity : list){
                if (activity == this){
                    l.add(activity);
                }
            }
            list.removeAll(l);
        }

//        RefWatcher refWatcher = UChengApplication.getRefWatcher(this);//1
//        refWatcher.watch(this);

    }

    /**
     * 重启app
     * */
    private void restartApp() {
        startActivity(new Intent(this, SlpashActivity.class));
        finish();
    }

}
